<?php

namespace app\store\model;

use app\common\model\Article as ArticleModel;

/**
 * 文章模型
 * Class Article
 * @package app\store\model
 */
class Article extends ArticleModel
{
    /**
     * 获取文章列表
     * @param int|null $category_id 分类ID
     * @param string|null $start_time 开始时间 (格式：Y-m-d)
     * @param string|null $end_time 结束时间 (格式：Y-m-d)
     * @return \think\Paginator
     * @throws \think\exception\DbException
     */
    public function getList($category_id = null, $start_time = null, $end_time = null,$not_category_id,$not_aritcle_id = null)
    {
        // dump($not_aritcle_id);die;
        $query = $this->with(['image', 'category'])
            ->where('is_delete', '=', 0);

        // 分类条件
        if (!empty($category_id)) {
            $query->where('category_id', '=', $category_id);
        }
        if ($not_category_id != null) {
            $query->where('category_id', 'neq', $not_category_id);
        }
        if ($not_aritcle_id != null) {
            $query->where('article_id', 'neq', $not_aritcle_id);
        }
        // 时间范围条件
        if (!empty($start_time)) {
            $start_timestamp = strtotime($start_time); // 转换为时间戳
            $query->where('create_time', '>=', $start_timestamp);
        }
        if (!empty($end_time)) {
            $end_timestamp = strtotime($end_time . ' 23:59:59'); // 转换为当天结束时间的时间戳
            $query->where('create_time', '<=', $end_timestamp);
        }

        return $query->order(['article_sort' => 'asc', 'create_time' => 'desc'])
            ->paginate(15, false, [
                'query' => request()->request()
            ]);
    }

    /**
     * 新增记录
     * @param $data
     * @return false|int
     */
    // public function add($data)
    // {
    //     if (empty($data['article_content'])) {
    //         $this->error = '请输入文章内容';
    //         return false;
    //     }
        
    //      // 商品图片
    //     $this->addGoodsImages($data['images']);//这里是我新增的方法
    //     $data['wxapp_id'] = self::$wxapp_id;
    //     return $this->allowField(true)->save($data);
    // }
    public function add($data)
    {
        if (empty($data['article_content'])) {
            $this->error = '请输入文章内容';
            return false;
        }

        $data['wxapp_id'] = self::$wxapp_id;

        // 先保存文章主体
        $result = $this->allowField(true)->save($data);
        if (!$result) {
            $this->error = '文章保存失败';
            return false;
        }

        // 再保存文章图片，传入文章ID
        if (isset($data['images'])) {
            $this->addGoodsImages($data['images'], $this['article_id']); // 这里传递 ID
        }
        return true;
    }
    private function addGoodsImages($images, $articleId)
    {
        $this->imagea()->delete(); // 删除旧的关联

        $data = array_map(function ($image_id) use ($articleId) {
            return [
                'article_id' => $articleId, // 关联文章
                'image_id' => $image_id,
                'wxapp_id' => self::$wxapp_id,
                'create_time' => time(),
            ];
        }, $images);

        return $this->imagea()->saveAll($data);
    }
    /**
     * 更新记录
     * @param $data
     * @return bool|int
     */
    public function edit($data,$article_id)
    {
        if (empty($data['article_content'])) {
            $this->error = '请输入文章内容';
            return false;
        }
        $data['wxapp_id'] = self::$wxapp_id;
        if (isset($data['images'])) {
            $this->addGoodsImages($data['images'], $article_id); // 这里传递 ID
        }
        return $this->allowField(true)->save($data) !== false;
    }

    /**
     * 软删除
     * @return false|int
     */
    public function setDelete()
    {
        return $this->save(['is_delete' => 1]);
    }

    /**
     * 获取文章总数量
     * @param array $where
     * @return int|string
     */
    public static function getArticleTotal($where = [])
    {
        $model = new static;
        !empty($where) && $model->where($where);
        return $model->where('is_delete', '=', 0)->count();
    }

}